# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: reporter\reports\basic_exporter.py
from reporter.reports.basic_template import ReportTemplate
import re
from cvsslib import CVSS3State, CVSS2State


class BasicExporter(ReportTemplate):
    RE_CWE = re.compile("CWE-(\\d+)")
    RE_CVE = re.compile("CVE-(\\d+)-(\\d+)")

    @staticmethod
    def extract_cwe_list(tags):
        cwe_list = []
        if tags:
            for tag in tags:
                m = BasicExporter.RE_CWE.match(tag)
                if m:
                    cwe_list.append({"id": m.group(1), "text": tag})

        return cwe_list

    @staticmethod
    def extract_cve_list(tags):
        cve_list = []
        if tags:
            for tag in tags:
                m = BasicExporter.RE_CVE.match(tag)
                if m:
                    cve_list.append({"year": m.group(1), "id": m.group(2), "text": tag})

        return cve_list

    def decode_cvss(self, cvss_string, version):
        decoded_cvss = dict()
        if version == "cvss3":
            state = CVSS3State()
            state.from_vector(cvss_string)
            scores = state.calculate()
            decoded_cvss["base_score"] = scores[0]
            decoded_cvss["attack_vector"] = state.attack_vector.name
            decoded_cvss["attack_complexity"] = state.attack_complexity.name
            decoded_cvss["privileges_required"] = state.privilege_required.name
            decoded_cvss["user_interaction"] = state.user_interaction.name
            decoded_cvss["scope"] = state.scope.name
            decoded_cvss["confidentiality_impact"] = state.confidentiality_impact.name
            decoded_cvss["integrity_impact"] = state.integrity_impact.name
            decoded_cvss["availability_impact"] = state.availability_impact.name
        else:
            state = CVSS2State()
            state.from_vector(cvss_string)
            scores = state.calculate()
            decoded_cvss["base_score"] = scores[0]
            decoded_cvss["access_vector"] = state.access_vector.name
            decoded_cvss["access_complexity"] = state.access_complexity.name
            decoded_cvss["authentication"] = state.authentication.name
            decoded_cvss["confidentiality_impact"] = state.confidentiality_impact.name
            decoded_cvss["integrity_impact"] = state.integrity_impact.name
            decoded_cvss["availability_impact"] = state.availability_impact.name
        return decoded_cvss
